<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="员工姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入员工姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- 单据类型 -->
      <el-form-item label="单据类型" prop="type">
        <!-- form_status -->
        <el-select v-model="queryParams.type" placeholder="请选择单据类型" clearable style="width: 200px">

          <el-option label="申购单" value="1" />
          <el-option label="采购单" value="2" />
          <el-option label="入库单" value="3" />
          <el-option label="出库单" value="4" />
          <el-option label="申领单" value="5" />
          <el-option label="请款单" value="6" />
          <el-option label="请款收据" value="7" />
          <el-option label="借款单" value="8" />
          <el-option label="借款收据" value="9" />
          <el-option label="报销单" value="10" />
          <el-option label="请假单" value="11" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center" prop="department" />

      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="单据类型" align="center" props="type">
        <template #default="scope">
          <dict-tag :options="form_status" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="单据详情" align="center" prop="reason">
        <template #default="scope">
          <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">单据详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="checkUser" />
      <el-table-column label="审批时间" align="center" prop="checkTime" />
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="" v-model="open" width="780px" append-to-body>


      <!-- <el-tabs v-model="componentId" type="card" @tab-click="handleClick">
        <el-tab-pane label="申购单" :name="shengoudan"></el-tab-pane>
        <el-tab-pane label="采购单" :name="caigoudan"></el-tab-pane>
        <el-tab-pane label="入库单" :name="rukudan"></el-tab-pane>
        <el-tab-pane label="出库单" :name="chukudan"></el-tab-pane>
        <el-tab-pane label="申领单" :name="shenlingdan"></el-tab-pane>
        <el-tab-pane label="请款单" :name="qingkuandan"></el-tab-pane>
        <el-tab-pane label="请款收据" :name="qingkuanshouju"></el-tab-pane>
        <el-tab-pane label="借款单" :name="jiekuandan"></el-tab-pane>
        <el-tab-pane label="借款收据" :name="jiekuanshouju"></el-tab-pane>
        <el-tab-pane label="报销单" :name="baoxiaodan"></el-tab-pane>
        <el-tab-pane label="请假单" :name="qingjiadan"></el-tab-pane>
      </el-tabs> -->


      <component :queryParams="form" :title="rowType" :is="componentId"></component>
    </el-dialog>


  </div>
</template>

<script setup name="Notice">
import { shallowRef } from 'vue'


/* 1申购单 2采购单 3入库单 4出库单 5申领单 6请款单 7请款收据 8借款单 9借款收据 10报销单 11请假单 */
/* 
在 Vue 3 中，shallowRef 是 Composition API 提供的一个新函数，
它用于创建一个响应式的引用（ref），但与普通的 ref 不同的是，
shallowRef 只会让引用对象本身响应式，而不会深度响应对象内部的嵌套数据。
这意味着，当你对 shallowRef 包裹的对象进行修改时，只有该对象的引用变化会引起视图更新，而对象内部的数据变化则不会。
*/
let componentId = shallowRef(shengoudan)


import shengoudan from '../common/1shengoudan.vue'
import caigoudan from '../common/2caigoudan.vue'
import rukudan from '../common/3rukudan.vue'
import chukudan from '../common/4chukudan.vue'
import shenlingdan from '../common/5shenlingdan.vue'
import qingkuandan from '../common/6qingkuandan.vue'
import qingkuanshouju from '../common/7qingkuanshouju.vue'
import jiekuandan from '../common/8jiekuandan.vue'
import jiekuanshouju from '../common/9jiekuanshouju.vue'
import baoxiaodan from '../common/10baoxiaodan.vue'
import qingjiadan from '../common/11qingjiadan.vue'


import {
  medicalApiFormDetail,
  listForm,
  getNotice,
  delNotice,
  updateNotice,
} from "@/api/backMedical/backMedical";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type, form_status } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type",
  "form_status"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);


const openDetail = ref(false)
// 禁用所有表单
const formDisable = ref(false)
const gridData = ref([])

let activeName = ref(1)
const handleClick = (tab, event) => {
  rowType.value = tab.props.label
}


let rowType = ref('')



/**详情按钮操作 */
function handleDetail(row) {
  // openDetail.value = !openDetail.value
  reset();
  const formId = row.formId || ids.value;
  medicalApiFormDetail(formId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "详情单据";
    // 根据单据类型显示不同的组件
    /* 1申购单 2采购单 3入库单 4出库单 5申领单 6请款单 7请款收据 8借款单 9借款收据 10报销单 11请假单 */
    switch (row.type) {
      case 1:
        componentId.value = shengoudan
        rowType.value = '申购单'
        break;
      case 2:
        componentId.value = caigoudan
        rowType.value = '采购单'
        break;
      case 3:
        componentId.value = rukudan
        rowType.value = '入库单'
        break;
      case 4:
        componentId.value = chukudan
        rowType.value = '出库单'
        break;
      case 5:
        componentId.value = shenlingdan
        rowType.value = '申领单'
        break;
      case 6:
        componentId.value = qingkuandan
        rowType.value = '请款单'
        break;
      case 7:
        componentId.value = qingkuanshouju
        rowType.value = '请款收据'
        break;
      case 8:
        componentId.value = jiekuandan
        rowType.value = '借款单'
        break;
      case 9:
        componentId.value = jiekuanshouju
        rowType.value = '借款收据'
        break;
      case 10:
        componentId.value = baoxiaodan
        rowType.value = '报销单'
        break;
      case 11:
        componentId.value = qingjiadan
        rowType.value = '请假单'
        break;
    }

    console.log(componentId.value, "componentId.value");

  });
  formDisable.value = true
}

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listForm(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>